#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_clicked()
{
    QMessageBox *messageBox = new QMessageBox(this);
    messageBox->setAttribute(Qt::WA_DeleteOnClose);
    messageBox->setWindowTitle("消息对话框");
    messageBox->setIcon(QMessageBox::Warning);
    messageBox->setText("这是一个消息对话框");

    //设置标准按钮
    messageBox->setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel | QMessageBox::Save);

    //设置自定义按钮
    QPushButton *button = new QPushButton("按钮");
    messageBox->addButton(button,QMessageBox::YesRole);

    connect(button,&QPushButton::clicked,this,&MainWindow::handle);

    //exec函数会返回用户点击了哪一个按钮
    int result = messageBox->exec();
    if(result == QMessageBox::Ok){
        qDebug() << "Ok";
    }else if(result == QMessageBox::Cancel){
        qDebug() << "Cancel";
    }else if(result == QMessageBox::Save){
        qDebug() << "Save";
    }
}

void MainWindow::handle()
{
    qDebug() << "按钮被按下";
}
